import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/pages/public/layout' // 公共部分

Vue.use(Router)
// 此处定义默认路由
const routes = [
  {
    path: '/',
    redirect: {
      path: '/index'
    },
    component: Layout,
    children: [
      {
        path: '/index',
        component: () => import('@/pages/index/index'),
        meta: {
          title: '后台首页',
          module: 'home'
        }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/pages/login/index'),
    meta: {
      title: '登录',
      needLogin: false
    }
  }
]

const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
  // 此处加入,防止路由抛出异常错误
  return routerPush.call(this, location).catch((error) => error)
}

const router = new Router({
  routes: routes
})
const modulesFiles = require.context('./modules', true, /\.js$/)

modulesFiles.keys().forEach((modulePath) => {
  const routerList = modulesFiles(modulePath)
  router.addRoutes(routerList.default)
})

export default router
